#include "CH58x_common.h"
#include "USB_kbd.h"
#include "config.h"
#include "HAL.h"
#include "device_config.h"
#include "USB/usbuser.h"
#include "RF_PHY/rf_config.h"
#include "RF_PHY/rf_port.h"

uint8_t USBTaskID = INVALID_TASK_ID;

static void USB_ProcessTMOSMsg(tmos_event_hdr_t *pMsg) {

    switch (pMsg->event) {
    case KEY_MESSAGE: {
        SendMSG_t *msg = (SendMSG_t *)pMsg;
        msg->hdr.status?tmos_set_event(USBTaskID, USB_IN_EVT):0;
        break;
    }
    default:
        break;
    }
}

void usb_send_release(void)
{
    const uint8_t key_normal_release[4 + 9] = {0};
    const uint8_t key_consume_release[4 + 3] = {0, 0, 0, 0, CONSUME_ID};
    const uint8_t key_bit_release[4 + 17] = {0, 0, 0, 0, KEYBIT_ID};
    const uint8_t key_mouse_release[4 + 5] = {0, 0, 0, 0, MOUSE_ID};

    lwrb_reset(&rf_ringbuf_data);

    if (is_host_sleep()) {
        return ;
    }

    PRINT("usb relese\n");
    lwrb_write(&rf_ringbuf_data, key_normal_release, sizeof(key_normal_release));
    lwrb_write(&rf_ringbuf_data, key_consume_release, sizeof(key_consume_release));
    lwrb_write(&rf_ringbuf_data, key_bit_release, sizeof(key_bit_release));

    /* Add more if you need */

    tmos_start_task(USBTaskID, USB_IN_EVT, MS1_TO_SYSTEM_TIME(100));
}

uint16_t USB_ProcessEvent(uint8 task_id, uint16 events) {

    if (events & SYS_EVENT_MSG) {
        uint8 *pMsg;

        if ((pMsg = tmos_msg_receive(USBTaskID)) != NULL) {
            USB_ProcessTMOSMsg((tmos_event_hdr_t *) pMsg);
            // Release the TMOS message
            tmos_msg_deallocate(pMsg);
        }
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
    }

    if(events & USB_IN_EVT){
        int ret; 
        if((ret = DevEPn_IN_Deal()) > 0 )
            tmos_start_task(USBTaskID, USB_IN_EVT, 1);
        else if(ret < 0)
            PRINT("usb in error: %d\n", ret);
        
        return (events ^ USB_IN_EVT);
    }

    if(events & USB_INIT_EVT){
        USB_Init();
        return (events ^ USB_INIT_EVT);
    }

    return 0;
}

void USB_Task_Init(void) {
    USBTaskID = TMOS_ProcessEventRegister(USB_ProcessEvent);
    tmos_start_task(USBTaskID, USB_INIT_EVT, MS1_TO_SYSTEM_TIME(CONFIG_RF_DG_PAIR_TO - 200));

//    tmos_start_task(USBTaskID, USB_TEST_EVT, MS1_TO_SYSTEM_TIME(1000));
}
